package yxy.game.pm2.active.command.weeklycard;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actweekcarkgrow;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_周卡;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.weeklycard.WeeklyActiveTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 周卡_目标任务 信息
 * @author Dell
 *
 */
@MessageType(proto = Command.ActWeeklyCardActiveBoxDrawReward, classtype = ActiveMessage.ActWeeklyCardActiveBoxDrawRewardRequest.class)
public class CMD_Active_周卡_活跃任务_BoxDrawReward extends Command<ActiveMessage.ActWeeklyCardActiveBoxDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_周卡_活跃任务_BoxDrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveBoxDrawRewardRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int id = request.getId();
		ActiveMessage.ActWeeklyCardActiveBoxDrawRewardResponse.Builder response = ActiveMessage.ActWeeklyCardActiveBoxDrawRewardResponse.newBuilder();
		
		ActiveModule_周卡 am = ActiveModule_周卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		WeeklyActiveTask wat = am.getWat();
		
		if(!wat.getGrowids().contains(id)) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(wat.getGrowgetids().contains(id)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		//
		int keynum = wat.getKeynum();
//		long point = Point.get(playercode, CurrencyType.CT_周卡_货币2);
		
		actweekcarkgrow awcg = ActDataCenter.getData(id, actweekcarkgrow.class, playercode);
		if(awcg == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(keynum < awcg.getCondition()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		Collection<Currency> curs = Currency.create(awcg.getRewardtype(), awcg.getRewardid(), awcg.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		
		response.addAllDisplay(items);

		wat.getGrowgetids().add(id);
		am.set();
		
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_周卡, curs);
 		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActWeeklyCardActiveBoxDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
